package gemini.castor.ui.client.page.content.office.messegercenter.form;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.utils.HtmlUtils;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class MessegerCenterFormView extends Composite implements
		MessegerCenterFormPresenter.Display {

	@UiTemplate("MessegerCenterFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, MessegerCenterFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;

	public MessegerCenterFormView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}

	@UiField
	FlexTable ftbMessage;

	@UiField
	Button btnRead;

	@UiField
	Button btnUnread;

	@UiField
	Button btnDelete;

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void loadData() {
		// TODO: NHS-Init data for order table
		initOrderList();
		initOrderTable();
		ftbMessage.removeAllRows();
		initMessageHeader();
		RowFormatter rowFmt = ftbMessage.getRowFormatter();
		int i = 1;
		for (String[] it : orderList) {
			final CheckBox chxSelect = new CheckBox();
			chxSelect.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

				@Override
				public void onValueChange(ValueChangeEvent<Boolean> event) {
					if (chxSelect.getValue()) {

					}
				}
			});
			ftbMessage.setWidget(i, 0, chxSelect);
			if (i % 2 == 0) {
				rowFmt.setStyleName(i, "rowEven");
			}
			for (int j = 1; j < 4; j++) {

				ftbMessage.setHTML(i, j, it[j - 1]);
			}
			i++;
		}

		// TODO: NHS-Init data for downline table
	}

	private void initOrderTable() {
		ColumnFormatter colFmt = ftbMessage.getColumnFormatter();

		colFmt.setWidth(0, "5%");
		colFmt.setWidth(1, "30%");
		colFmt.setWidth(2, "50%");
		colFmt.setWidth(3, "15%");
	}

	private void initMessageHeader() {
		final CheckBox chxSelect = new CheckBox();
		chxSelect.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				if (chxSelect.getValue()) {

				}
			}
		});
		ftbMessage.setWidget(0, 0, chxSelect);
		ftbMessage.setHTML(0, 1, HtmlUtils.bold(constants.htmFrom()));
		ftbMessage.setHTML(0, 2, HtmlUtils.bold(constants.htmSubject()));
		ftbMessage.setHTML(0, 3, HtmlUtils.bold(constants.htmDateReceiv()));
		ftbMessage.getRowFormatter().setStyleName(0, "tableHeader");
		ftbMessage.setStyleName("ftbSummary");
	}

	private List<String[]> orderList = new ArrayList<String[]>();

	private void initOrderList() {
		orderList.clear();
		String[] st1 = { "Nguyen Thanh Binh", "The new price list", "03/02" };
		String[] st2 = { "Herbalife", "Please check your cart", "01/02" };
		String[] st3 = { "Duong Hoai Vu", "Please order for me", "10/01" };
		String[] st4 = { "Herbalife", "Nguyen Thanh Binh", "09/01" };
		String[] st5 = { "Truong Khac Xuan", "I want to be a distributor",
				"01/01" };
		orderList.add(st1);
		orderList.add(st2);
		orderList.add(st3);
		orderList.add(st4);
		orderList.add(st5);
	}

}
